﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Senthil.Mobile.WhereAmI
{
    public partial class SoundConfigureForm : Form
    {
        public SoundConfigureForm()
        {
            InitializeComponent();
            timeSpanToDestinationTextBoxOriginalColor = timeSpanToDestinationTextBox.BackColor;
        }

        string soundFileName;
        string locationGroupName;
        TimeSpan timeSpanToDestination;

        Color timeSpanToDestinationTextBoxOriginalColor;

        public LocationGroup[] LocationGroups {get;set;}

        public bool SoundNotificationsEnabled
        {
            get { return enableSoundsOnEvents.Checked; }
            set { enableSoundsOnEvents.Checked = value; }
        }

        public string SoundFileName
        {
            get { return soundFileName; }
            set { soundFileName = value; }
        }

        public TimeSpan TimeSpanToDestination
        {
            get { return timeSpanToDestination; }
            set { timeSpanToDestination = value; }
        }

        public string LocationGroupName { get { return locationGroupName; } set { locationGroupName = value; } }


        private void panel2_GotFocus(object sender, EventArgs e)
        {

        }

        private void SoundConfigureForm_Load(object sender, EventArgs e)
        {
            soundFilePathLabel.Text = soundFileName;
            timeSpanToDestinationTextBox.Text = timeSpanToDestination.ToString();
            LoadLocationGroups();
        }

        private void LoadLocationGroups()
        {
            int indexToSelect = 0;
            locationGroupComboBox.Items.Add("");

            foreach (LocationGroup lg in LocationGroups)
            {
                if (!string.IsNullOrEmpty(lg.Name))
                {
                    int index = locationGroupComboBox.Items.Add(lg);

                    if (lg.Name == locationGroupName)
                    {
                        indexToSelect = index;
                    }
                }
            }

            locationGroupComboBox.SelectedIndex = indexToSelect;
        }

        private void okMenuItem_Click(object sender, EventArgs e)
        {
            if (SoundNotificationsEnabled && !System.IO.File.Exists(soundFileName))
            {
                MessageBox.Show(soundFileName + " does not exist. Please select a valid sound file name, or disable sounds.", "Invalid FileName");
                return;    
            }

            this.DialogResult = DialogResult.OK;
        }

        private void cancelMenuItem_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void locationGroupComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (locationGroupComboBox.SelectedItem != null)
            {
                LocationGroup group = locationGroupComboBox.SelectedItem as LocationGroup;

                if (group != null)
                    locationGroupName = group.Name;
                else
                    locationGroupName = "";
            }
        }

        private void timeSpanToDestinationTextBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                timeSpanToDestination = TimeSpan.Parse(timeSpanToDestinationTextBox.Text);
                timeSpanToDestinationTextBox.BackColor = timeSpanToDestinationTextBoxOriginalColor;
            }
            catch (Exception)
            {
                timeSpanToDestinationTextBox.BackColor = Color.LightPink;
            }
        }

        private void soundFilePathLabel_Click(object sender, EventArgs e)
        {
            using (CustomOpenFileDialog ofd = new CustomOpenFileDialog())
            {
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    soundFilePathLabel.Text = soundFileName = ofd.SelectedFileName;
                }
            }
        }
    }
}